package com.kdgz.kdgz.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kdgz.kdgz.beans.Finance;
import com.kdgz.kdgz.mapper.FinanceMapper;
import com.kdgz.kdgz.sevice.FinanceService;
import com.kdgz.kdgz.sevice.OrdersService;
import com.kdgz.kdgz.sevice.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author Veneno
 * @Desc 模拟每月自动统计账单
 * @date 2021/3/20
 */
@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    FinanceMapper financeMapper;
    @Autowired
    OrdersService ordersService;
    @Autowired
    StaffService staffService;
    @Override
    public void insertData(Integer year, Integer month) {
        BigDecimal income = ordersService.getTotalPrice(year, month);
        BigDecimal expenditure = staffService.getTotalSalary().add(new BigDecimal(ordersService.totalOrdersByMonth(year,month)*0.5));
        Finance finance = new Finance();

        Calendar calendar = Calendar.getInstance();
        //设置年份
        calendar.set(Calendar.YEAR,year);
        //设置月份
        calendar.set(Calendar.MONTH, month);
        //获取该月最后一天
        calendar.set(Calendar.DAY_OF_MONTH,0);
        Date time = calendar.getTime();
        finance.setIncome(income);
        finance.setExpenditure(expenditure);
        finance.setDate(time);
        financeMapper.insert(finance);
    }

    @Override
    public List<Finance> getAllFinance() {
        List<Finance> finances=financeMapper.selectList(null);
        Collections.sort(finances);
        return finances;
    }

    @Override
    public List<Finance> getFinanceByYear(Integer year) {
        return financeMapper.getFinanceByYear(year);
    }

    @Override
    public Finance getFinanceByMonth(Integer year, Integer month) {
        return financeMapper.getFinanceByMonth(year,month);
    }
}
